package com.sirui.x.io.netty;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

public class NettyHttpServerWrapper {

	private static final Logger logger = LoggerFactory.getLogger(NettyHttpServerWrapper.class);
	
	private static final int PORT = 4010;
	
	public void start() throws InterruptedException {
		Utf8ServerHandle utf8ChildHandle = new Utf8ServerHandle();
		ServerBootstrap b = new ServerBootstrap();
		EventLoopGroup parentGroup = new NioEventLoopGroup();
		EventLoopGroup childGroup = new NioEventLoopGroup();
		//ChannelOption.SO_BACKLOG是输入连接指示（对连接的请求）的最大队列长度被设置为 backlog 参数。如果队列满时收到连接指示，则拒绝该连接。
		b.group(parentGroup, childGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 1024).localAddress(PORT).handler(new ChannelInitializer<NioServerSocketChannel>() {
            protected void initChannel(NioServerSocketChannel ch) {
            	logger.info("parent handle...");
            	
            }
        }).childHandler(new ChannelInitializer<NioSocketChannel>() {
            protected void initChannel(NioSocketChannel ch) {
            	logger.info("child handle...");
            	ch.pipeline().addLast(utf8ChildHandle);
            }
        });
		
		b.bind().sync();
		logger.info("start");
	}
	
	public static void main(String[] args) throws InterruptedException {
		new NettyHttpServerWrapper().start();
	}
}
